package com.app.backend.common.base;

import cn.hutool.core.util.StrUtil;
import com.app.backend.common.enums.OrderType;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.Transient;
import java.io.Serializable;
import java.util.Arrays;

/**
 * @program: PageEntity
 * @description:
 * @author: Zhun.Xiao
 * @create: 2020-02-10 12:55
 **/
@Data
public class PageEntity implements Serializable {


    /**
     * 下面两个方便查询用
     */
    @ApiModelProperty(value = "起始时间")
    @Transient
    private String startTime;

    @ApiModelProperty(value = "结束时间")
    @Transient
    private String endTime;


    @ApiModelProperty(value = "页号")
    @Transient
    private int pageNumber = 0;

    @ApiModelProperty(value = "页面大小")
    @Transient
    private int pageSize = 10;

    @ApiModelProperty(value = "排序字段")
    @Transient
    private String sort = "id";

    @ApiModelProperty(value = "排序方式 asc/desc")
    @Transient
    private String order = "desc";


    /**
     * 根据传入生成pageEntity
     * @param t
     * @param <T>
     * @return
     */
    public static <T extends PageEntity> PageEntity valueOf(T t) {
        PageEntity pageEntity = new PageEntity();
        if (!PageEntity.class.isAssignableFrom(t.getClass())) {
            return pageEntity;
        }
        pageEntity.setPageNumber(t.getPageNumber());
        pageEntity.setPageSize(t.getPageSize());
        if (StrUtil.isNotEmpty(t.getOrder())
                && Arrays.stream(OrderType.values()).anyMatch(x -> x.equals(t.getOrder()))) {
            pageEntity.setOrder(t.getOrder());
        }
        if (StrUtil.isNotEmpty(t.getSort())) pageEntity.setSort(t.getSort());
        return pageEntity;

    }




}